package cn.web.management.web.servlet.inform.user;

import cn.web.management.domain.Salary;
import cn.web.management.service.impl.QuerySalaryImpl;
import cn.web.management.service.inface.QuerySalary;
import net.sf.json.JSONArray;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "querySalaryServlet", value = "/querySalaryServlet")
public class querySalaryServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    /**
     * @description: 用户查询薪资信息
     * @paramName: [req, resp]
     * @paramType: [javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse]
     * @returnType: void
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        int page = Integer.valueOf(req.getParameter("page"));
        int limit = Integer.valueOf(req.getParameter("limit"));

        HttpSession session = req.getSession();
        String queryWay = req.getParameter("queryWay");
        String queryDate = req.getParameter("queryDate");

        String employno = (String) session.getAttribute("employno");
        QuerySalary querySalary = new QuerySalaryImpl();
        List<Salary> salary = null;

        if (queryWay.equals("month"))
            salary = querySalary.findSalary(employno, queryDate);
        else {
             salary = querySalary.findSalary(employno);
        }

        String queryResult = "{\"code\":0,\"msg\":\"\",\"count\":"+salary.size()+",\"data\":";

        int min = limit*(page-1);
        int max = limit*page;
        if (max>salary.size())
            max = salary.size();

        //分页处理
        salary = salary.subList(min,max);

        JSONArray jsonArray = JSONArray.fromObject(salary);
        queryResult +=jsonArray.toString();
        queryResult+="}";
        resp.getWriter().write(queryResult);
    }
}
